package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class AdChannelServiceImpl  extends ServiceImpl<AdChannelMapper, AdChannel> implements  AdChannelService {


    @Override
    public ResponseResult list(ChannelDto dto) {
            //1.判断是否有参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数 并设置默认值
        dto.checkParam();
        LambdaQueryWrapper<AdChannel> Wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            Wrapper.like(AdChannel::getName,dto.getName());
        }
        //执行分页查询
        IPage<AdChannel> page = new Page(dto.getPage(), dto.getSize());
        this.page(page,Wrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        if(channel==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channel.setIsDefault(false);
        channel.setCreatedTime(new Date());
        this.save(channel);
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updatessss(AdChannel channel) {
        //1.判断整体参数是否为空
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id 判断 是否存在
        AdChannel byId = this.getById(channel);
        if(byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道数据不存在");
        }
        //根据id更新频道
         this.updateById(channel);
        //4.响应数据

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
            //判断参数是否违法
        if(id==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel byId = this.getById(id);
        if(byId==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        if(byId.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIC_ERROR, "频道已启用不能删除");

        }
           this.removeById(id);

        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
